package com.zpself.module.算法练习.链表;

import java.util.*;

/**
 * @author By ZengPeng
 * @Description
 * @date in  2020/10/14 11:12
 * @Modified By
 */
public class 删除连续数字 {
    public static void main(String[] args) {
        int[] ints = delRepeat(new int[]{4, 5, 5,5, 4, 3, 2} ,3);
        for (int i = 0; i < ints.length; i++) {
            int anInt = ints[i];
            System.out.print(anInt+",");
        }
    }
    private static int[] delRepeat(int[] target,int k){
        Deque<Integer> deque = new ArrayDeque<>();
        Stack<Integer> save = new Stack<>();
        for (int aTarget : target) {
            if (deque.isEmpty()) {deque.push(aTarget);continue; }
            Integer top = deque.peek();
            if (top != aTarget)deque.push(aTarget);
            else {
                for (int j = 0; j < k - 1; j++) {
                    if (deque.isEmpty()) break;
                    Integer pop = deque.pop();
                    save.push(pop);
                    if (pop != aTarget) {
                        deque.addAll(save);
                        deque.push(aTarget);
                        save.clear();break; } }
            }
        }
        int[] r = new int[deque.size()];
        for (int i = 0; i < r.length; i++)
            r[i]=deque.pollLast();
        return r ;
    }




}

